clear 
set more off 
set matsize 8000 
cap log close 

set seed 100

cd "D:\Data\workdata\707116\SIDS_RDD\"

use "SIDS_analysis_final_full.dta", clear

log using "Revision1/log_files/02 Randomization days.log", replace

drop di*
g dif = birthday - mdy(12,31,1991)

gen     di1 =   dif 
gen     di1_i = dif 
replace di1_i = 0 if dif<0

g dc_sids_oth = dc_sids + dc_all_other 

g 		mor1_exc_sids = mor1 - dc_sids_oth
replace mor1_exc_sids = 0 if mor1==0 

g 		mor5_exc_sids = mor5 - dc_sids_oth
replace mor1_exc_sids =0 if mor5==0 

g m_high_ed = m_short + m_mid + m_long
egen dc_other_non_sids = rowmax(dc_inf  dc_neo dc_nerv dc_resp)

g preterm = . 
replace preterm = 1 if fullterm37==0
replace preterm = 0 if fullterm37==1


label var mage "Mother age at birth" 
label var m_basic "Mother basic education" 
label var m_voc "Mother vocational education" 
label var m_high "Mother higher education" 
label var m_couple "Mother married or living together"
label var m_immigrant "Mother immigrant"  

label var female "Female" 
label var border "Birth order" 
label var preterm "Born preterm"
label var LBW "Low birthweight" 

label var dc_sids "SIDS mortality (over 10K)" 
label var dc_sids_oth "SIDS + all unclassified (over 10K)"
label var mor1 "Infant mortality" 
label var mor5 "Child mortality" 
label var mor1_exc_sids "Infant mortality except SIDS" 
label var mor5_exc_sids "Child mortality except SIDS" 


global cset1 "female border log_bw LBW gage_wks preterm"
global cset2 "mage m_basic m_voc m_high m_couple m_immigrant"
global outset1 "dc_sids dc_sids_oth mor1 mor5 mor1_exc_sids mor5_exc_sids"
global serr "bym"


rdbwselect mor1 dif, bwselect(mserd)  
local bw = round(e(h_mserd)) 



forv x=1/201{
	drop di1* treat
	g dif_placebo = dif- (`x'*30.5) + 101*30.5
	replace dif_placebo = round(dif_placebo)
	table bym if inrange(dif_placebo, -60,60), stat(mean dif_placebo) stat(mean dif)
    g treat = 0 
	replace treat = 1 if dif_placebo>0
	gen di1 = dif_placebo 
	gen di1_i=dif_placebo 
	replace di1_i=0 if dif_placebo<0
	reg mor1 treat di1* if inrange(dif_placebo,-`bw',`bw'), cluster(birthday)	
	g N__`x' = e(N)
	test treat 
	g b__`x' = _b[treat]
	g se__`x' = _se[treat]
	g p__`x' = r(p)
	qui sum mor1 if e(sample) & treat==0
	g M__`x' = r(mean)
	qui sum birthday if e(sample), det 
	g min_ym__`x' = r(min)
	g max_ym__`x' = r(max)
	qui sum birthday if e(sample) & dif_placebo==0
	g med_ym__`x' = r(mean)
	drop dif_placebo 
}


keep b__* se__* p__* N__* M__* min_ym__* max_ym__* med_ym__*
keep if _n==1 
xpose, clear varname 
rename v1 v_scalar 
split _varname, p("__")
drop _varname 
rename _varname1 coef_name 
rename _varname2 dif_relative 
destring dif_relative, replace

g min_ym = . 
g max_ym = . 
g med_ym = . 

forv x=1/201{
	qui sum v_scalar if coef_name=="min_ym" & dif_relative==`x'
	replace min_ym = r(mean) if dif_relative==`x'
	qui sum v_scalar if coef_name=="max_ym" & dif_relative==`x'
	replace max_ym = r(mean) if dif_relative==`x'
	qui sum v_scalar if coef_name=="med_ym" & dif_relative==`x'
	replace med_ym = r(mean) if dif_relative==`x'
}

format min_ym max_ym med_ym %td


export excel using "Revision1\output\days\Rob_randomization.xlsx", firstrow(variables) replace 
 
log close 